import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Users from '../views/Users.vue'
import Welcom from '../views/Welcom.vue'
import Rights from '../views/Rights.vue'
import Roles from '../views/Roles.vue'
import Categories from '../views/Categories.vue'
import Reports from '../views/Reports.vue'
import Goods from '../views/Goods.vue'
import Params from '../views/Params.vue'
import Orders from '../views/Orders.vue'

import Test from '../views/Test.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/test',
    name: 'test',
    component: Test
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    redirect: '/home',
    children: [
      { 
        path: '/home',
        component: Welcom
      },
      { 
        path: '/users',
        name: 'users',
        component: Users
      },
      { 
        path: '/rights',
        name: 'rights',
        component: Rights
      },
      { 
        path: '/roles',
        name: 'roles',
        component: Roles
      },
      { 
        path: '/categories',
        name: 'categories',
        component: Categories
      },
      { 
        path: '/reports',
        name: 'reports',
        component: Reports
      },
      { 
        path: '/goods',
        name: 'goods',
        component: Goods
      },
      { 
        path: '/params',
        name: 'params',
        component: Params
      },
      { 
        path: '/orders',
        name: 'orders',
        component: Orders
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const isToken = sessionStorage.getItem('token') || ''

  if (to.name !== 'login' && !isToken) {
    return next({ name: 'login' })
  }

  next()
})

export default router
